package com.yowasa.xiuxian.engine.message.map;

import com.yowasa.xiuxian.annoations.CMD;
import com.yowasa.xiuxian.engine.message.CmdMessageHandler;
import com.yowasa.xiuxian.entity.bo.user.UserBO;
import com.yowasa.xiuxian.entity.enums.CmdScopeEnum;
import com.yowasa.xiuxian.entity.enums.MapEnum;
import com.yowasa.xiuxian.entity.mys.MysComponent;
import com.yowasa.xiuxian.entity.mys.SendMessage;
import com.yowasa.xiuxian.entity.po.XiuxianMapEnemyPO;
import com.yowasa.xiuxian.exception.HandlerException;
import com.yowasa.xiuxian.service.helper.LocalHelper;
import com.yowasa.xiuxian.service.helper.MessageHelper;
import com.yowasa.xiuxian.service.helper.UserHelper;
import com.yowasa.xiuxian.util.ComponentUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;

import java.text.MessageFormat;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@CMD(name = "/前往", scope = CmdScopeEnum.ALL)
public class MoveHandler extends CmdMessageHandler {
    @Autowired
    private UserHelper userHelper;
    @Autowired
    private MessageHelper messageHelper;
    @Autowired
    private LocalHelper localHelper;

    @Override
    public void execute(SendMessage message) {
        UserBO userBO = userHelper.checkOpUserAll(message);
        MapEnum mapEnum = MapEnum.getById(userBO.getLocal());
        String param = getParam(message);
        List<Integer> path = mapEnum.getPath();
        if (StringUtils.isEmpty(param)) {
//            messageHelper.imageSendMys(message,"https://upload-bbs.miyoushe.com/upload/2023/10/16/254576762/BZLaHAg6dzWf3s9C/1697444474739.jpg");
            messageHelper.imageSendMys(message,"https://upload-bbs.miyoushe.com/upload/2024/2/7/254576762/QLLI5ozonSnhKypf/daoqiMap.png");
            Stream<String> stringStream = path.stream().map(e -> MapEnum.getById(e).getName());
            List<String> collect = stringStream.collect(Collectors.toList());
            List<List<MysComponent>> componentList = ComponentUtil.getComponentList("/前往 ",2,collect);
            messageHelper.replyMysPanel(message, "你可以前往\n"+String.join("\n",collect),null,componentList,null);
            return;
        }
        MapEnum target = MapEnum.getByName(param);
        if (target == null) {
            throw new HandlerException(MessageFormat.format("未识别的地名[{0}]", param));
        }
        if (!path.contains(target.getId())) {
            throw new HandlerException(MessageFormat.format("当前地点无法前往[{0}]", param));
        }
        userBO.setLocal(target.getId());
        userBO.setLocalName(target.getName());
        int days = calCostDay(mapEnum, target, userBO.getUserBattle().getSpd());
        if (userBO.getUserConfig().getStudiedSpecialEffect().contains("千里神行")) {
            days = days / 2;
        }
        userHelper.lockUser(userBO, days, MessageFormat.format(userBO.getName() + "正在前往{0}", param));
        userHelper.saveUserInfo(userBO);
        String format = MessageFormat.format("{0}花费了{1}天前往了[{2}]\n", userBO.getName(), days, param);
        if(target.getType() == 3){
            List<XiuxianMapEnemyPO> enemyPOS = localHelper.selectLocalEnenmy(target.getId());
            StringBuffer sb =new StringBuffer();
            enemyPOS.stream().forEach(e->sb.append(e.getEnemyName()).append(","));
            format += ("此处可能出现的怪物有:\n"+sb.toString());
        }
        messageHelper.replyMys(message, format);
    }



    private int calCostDay(MapEnum a, MapEnum b, int speed) {
        int x1 = a.getX();
        int y1 = a.getY();
        int x2 = b.getX();
        int y2 = b.getY();
        int length = Math.abs(x1-x2) + Math.abs(y1-y2);
        return length / speed;

    }

    public String desc(){
        return new StringBuffer().append("在地图上移动的指令，移动需要消耗时间\n")
                .toString();

    }
}
